package priv.amos.androidnetwork.datapicker;

import java.util.Date;
import java.util.List;

import cz.msebera.android.httpclient.NameValuePair;
import priv.amos.androidnetwork.bean.ResponseData;
import priv.amos.androidnetwork.buffer.BufferUtils;
import priv.amos.androidnetwork.connector.DataNetWorkConnector;
import priv.amos.androidnetwork.exception.ReInitedException;
import priv.amos.androidnetwork.exception.UnInitedException;
import priv.amos.androidnetwork.filter.DataFilter;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class DataConnector {
	private static boolean inited;
	private static DataNetWorkConnector dataConnector;
	private static Context context;
	private static DataFilter dataFilter;
	public static void init(Context ctx,int connThreadsSize,int connTimeout,int soTimeout) throws ReInitedException,IllegalArgumentException{
		if(inited){
			throw new ReInitedException("Cannot init twice");
		}
		if(ctx==null||connThreadsSize<=0||connTimeout<=0||soTimeout<=0){
			throw new IllegalArgumentException("ctx is null or connThreadsSize<=0 or connTimeout<=0 or soTimeout<=0");
		}
		inited=true;
		dataConnector=new DataNetWorkConnector(ctx,connThreadsSize,connTimeout,soTimeout);
		context=ctx;
	}
	public static void startConnCookies() throws UnInitedException{
		if(inited){
			throw new UnInitedException("Haven't inited, you need to invoke init method of DataProcessor first");
		}
		dataConnector.startNetworkCookies(context);
	}
	public static synchronized void getData(String url,String dataType,int timeoutInMiliSecs,Handler h) throws UnInitedException{
		if(!inited){
			throw new UnInitedException("Haven't inited, you need to invoke init method of DataProcessor first");
		}
		if(timeoutInMiliSecs<0||(new Date()).getTime()>(BufferUtils.getLastUpdate(context, url)+timeoutInMiliSecs)){
			dataConnector.getNetWorkData(url, dataType, new DataSaver(context,url,dataType,h),timeoutInMiliSecs);
		}
		else{
			Object obj=BufferUtils.getData(context, url, dataType);
			Message msg=new Message();
			msg.obj=obj;
			h.sendMessage(msg);
		}
	}
	public static synchronized void postData(String url,String dataType,List<NameValuePair> nvps,Handler h) throws UnInitedException{
		if(!inited){
			throw new UnInitedException("Haven't inited, you need to invoke init method of DataProcessor first");
		}
		dataConnector.postNetWorkData( url, dataType, nvps, h);
	}
	public static void registFilter(DataFilter dataFilter){
		DataConnector.dataFilter=dataFilter;
	}
	private static class DataSaver extends Handler{
		private String url;
		private String dataType;
		private Context ctx;
		private Handler h;
		public DataSaver(Context ctx,String url,String dataType,Handler h){
			this.url=url;
			this.dataType=dataType;
			this.ctx=ctx;
			this.h=h;
		}
		@Override
		public void handleMessage(Message msg){
			BufferUtils.saveData(ctx, url, msg.obj, dataType);
			Message mes=new Message();
			ResponseData resData=(ResponseData)msg.obj;
			if(dataFilter!=null){
				dataFilter.filtData(resData);
			}
			mes.obj=resData;
			h.sendMessage(mes);
		}
	}
}
